﻿using HPIT.Shop.Services.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace HPIT.Shop.Services
{
    
    public class BaseService<T> where T : class
    {
        //在BaseServiece中不实例化上下文，将实例化上下文交给调用方
        private ShopContext db;

        public BaseService(ShopContext db)
        { 
            this.db = db;
        }
        /// <summary>
        /// 添加实体返回数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public long Add(T entity) 
        {
            db.Entry<T>(entity).State = EntityState.Added;
            return db.SaveChanges();
        }
        public bool Update(T entity) 
        {
            //将EF对象添加到容器中
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State &= EntityState.Modified;
            return db.SaveChanges() > 0;
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            //将EF对象添加到容器中
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State &= EntityState.Deleted;
            return db.SaveChanges() > 0;
        }
        /// <summary>
        /// 查询实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public T Get(Expression<Func<T, bool>> whereLambda)
        {
            return GetList(whereLambda).FirstOrDefault();
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> GetList(Expression<Func<T, bool>> whereLambda)
        {
            return db.Set<T>().Where(whereLambda);
        }
        /// <summary>
        /// 查询类别支持排序
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="whereLambda"></param>
        /// <param name="orderbtLamba"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetList<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderbtLamba, bool isAsc = true)
        {
            if (isAsc)
            {
                return GetList(whereLambda).OrderBy(orderbtLamba);
            }
            else
            {
                return GetList(whereLambda).OrderByDescending(orderbtLamba);
            }
        }
        /// <summary>
        /// 查询列表支持分页
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="start"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderbtLamba"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetPageList<TKey>(int start, int pageSize, ref int rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderbtLamba, bool isAsc = true)
        {
            //获取记录总数
            rowCount = GetList(whereLambda).Count();

            //边界值判断
            start = start < 0 ? 0 : start;

            if (isAsc)
            {
                return db.Set<T>().Where(whereLambda).OrderBy(orderbtLamba).Skip(start).Take(pageSize);
            }
            else
            {
                return db.Set<T>().Where(whereLambda).OrderByDescending(orderbtLamba).Skip(start).Take(pageSize);
            }
        }

    }
}
